﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AlgorithmBox.FlowCharts
{
	/// <summary>
	/// Interaction logic for FlowChartElement.xaml
	/// </summary>
	public abstract partial class FlowChartElement : UserControl, ISelectable
	{
		public virtual string Text
		{
			get { return ContentBlock.Text; }
			set { ContentBlock.Text = value; }
		}

		public ConnectionLine Link;
		public List<ConnectionLine> PrevLink = new List<ConnectionLine>();

		public uint ID;
		static uint id;

		#region ISelectable Members

		bool isSelected = false;
		public bool IsSelected
		{
			get { return isSelected; }
			set
			{
				isSelected = value;
				if (isSelected)
					this.FlowBorder.BorderBrush = (SolidColorBrush)App.Current.Resources["SelectedBrush"];
				else
				{
					this.FlowBorder.BorderBrush = (SolidColorBrush)App.Current.Resources["FlowBorderBrush"];
					ApplyText();
				}
			}
		}

		#endregion

		public FlowChartElement()
		{
			InitializeComponent();
			this.ID = id++;
			this.ContentChanger.TextChanged +=new TextChangedEventHandler(ContentChanger_TextChanged);
		}

		private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			e.Handled = true;

			this.Text = String.Empty;
			ContentChanger.Visibility = Visibility.Visible;
			ContentChanger.Focus();
		}

		private void ContentChanger_LostFocus(object sender, RoutedEventArgs e)
		{
			ApplyText();
		}

		protected virtual void ContentChanger_TextChanged(object sender, TextChangedEventArgs e)
		{
			//if (this is Decision)
			//{
			//    this.Width = this.ContentChanger.Height + 50;
			//    this.Height = this.ContentChanger.Height + 50;
			//}
			//else
			//{
			//    this.Width = this.ContentChanger.Width;
			//    this.Height = this.ContentChanger.Height;
			//}
		}

		private void ContentChanger_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyboardDevice.IsKeyDown(Key.LeftShift) && e.Key == Key.Enter)
			{
				this.ContentChanger.Text += Environment.NewLine;
				this.ContentChanger.SelectionStart = this.ContentChanger.Text.Length - 1;
			}
			else if (e.Key == Key.Enter)
			{
				ApplyText();
			}
		}

		void ApplyText()
		{
			if (this.IsEnabled)
			{
				this.Text = ContentChanger.Text;
				ContentChanger.Visibility = Visibility.Hidden;
			}
		}

		protected virtual void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
		{
			if (!Double.IsNaN(e.NewSize.Width) && !Double.IsNaN(e.PreviousSize.Width))
			{
				this.SetValue(Canvas.LeftProperty,
					(double)this.GetValue(Canvas.LeftProperty) - (e.NewSize.Width - e.PreviousSize.Width)/2);

				if (this is Decision)
				{
					this.SetValue(Canvas.TopProperty,
						(double)this.GetValue(Canvas.TopProperty) - (e.NewSize.Height - e.PreviousSize.Height)/2);
				}
			}
		}
	}
}
